<?php
declare (strict_types = 1);

namespace app\api\model\yuql;

use think\Model;

/**
 * @mixin \think\Model
 */
class Seckill extends Model
{
    protected $table = '2008a_store_seckill';

    /**
     * yuql
     * 秒杀商品查询 sql
     * @return Seckill[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function goodsListModel($data){  //搜索
        $data = Seckill::where('title','like','%'.$data.'%')
            ->order('id','desc')
            ->paginate(10);
        return $data;
    }
    public static function goodsListModels(){  //未搜索
        $data = Seckill::order('id','desc')
            ->paginate(10);
        return $data;
    }


    /**
     * yuql
     * 商品添加
     * @param $data
     * @return Seckill|Model
     */
    public static function goodsAddModel($data){
        $result = Seckill::create($data);
        return $result;
    }

    /**
     * yuql
     * 商品详情 查询本条数据
     * @param $data
     * @return Seckill|array|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function goodsDetailOne($data){
        $result = Seckill::where($data)->find();
        return $result;
    }

    /**
     * yuql
     * 修改商品库存
     * @param $endNum
     * @param $query
     * @return Seckill|array|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function goodsNumUpdate($endNum,$query){
        $data = Seckill::where($query)->find();
        $data->goods_num = $endNum;
        $data->save();
        return $data;
    }

    /**
     * yuql
     * 根据用户买过得商品订单查询所有商品
     * @param $data
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public static function RecommendGoodsList($data){
        $data = Seckill::where($data)->paginate(5)->toArray();
        return $data;
    }

    /**
     * yuql
     * 根据用户搜索商品名称查询库存
     * @param $data
     * @return Seckill[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function userSearchGoods($data){
        $result = Seckill::where('title','like','%'.$data.'%')
            ->select()
            ->toArray();
        return $result;
    }

    /**
     * yuql
     * 根据15分未支付的商品修改商品库存
     * @param $data
     * @param $plus
     * @return Seckill|array|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function updateGoodsNum($data,$plus){
        $result = Seckill::where($data)->find();
        $result->goods_num += $plus;
        $result->save();
        return $result;
    }

}
